package cn.itcast.bos.web.action.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.apache.poi.ss.formula.functions.T;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

	private static final long serialVersionUID = 1L;
	
	protected int page;
	protected int rows;
	
	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	protected T model;
	
	@Override
	public T getModel() {
		return model;
	}
	
	// 泛型转换（获取子类的真实类型进行实例化）------------构造器 完成model实例化
	public BaseAction(){
		// 构造子类Action对象 ，获取继承父类型的泛型，然后实例化
		// AreaAction extends BaseAction<Area>
		// 需要获取BaseAction<Area>中的Area，然后实例化
		
		//this.getClass()子类.getGenericSuperclass()获取父类的类型的对象
		Type type = this.getClass().getGenericSuperclass();
		
		ParameterizedType parameterizedType = (ParameterizedType) type;//强转成参数类型BaseAction<Area>
		Class entityClass = (Class) parameterizedType.getActualTypeArguments()[0];//表示获取第一个泛型（有可能子类有多个）
		try {
			model = (T) entityClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
}
